﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmDonViTinh : Form
    {
        DonViTinhBL donvitinhbl=new DonViTinhBL();
        private int trangthai = 0;

        public FrmDonViTinh()
        {
            InitializeComponent();
        }

        private void FrmDonViTinh_Load(object sender, EventArgs e)
        {
            gvDVT.DataSource = donvitinhbl.LayDanhSachDVT();
            AnHienButton(false);
            btXoaDVT.Enabled = false;
        }

        private void AnHienButton(bool flag)
        {
            btThemDVT.Enabled = !flag;
            btSuaDVT.Enabled = !flag;
            //btXoaDVT.Enabled = !flag;
            btOk.Enabled = flag;
            btHuy.Enabled = flag;

            tbMaDVT.Enabled = flag;
            tbTenDVT.Enabled = flag;

            gvDVT.Enabled = !flag;
        }

        private void CapNhatText()
        {
            tbMaDVT.Text = "";
            tbTenDVT.Text = "";
        }

        private DonViTinhBO LayThongTinDVT()
        {
            DonViTinhBO donvitinhbo = new DonViTinhBO();
            donvitinhbo.MaDVT = tbMaDVT.Text;
            donvitinhbo.TenDVT = tbTenDVT.Text;
            return donvitinhbo;
        }

        private void btThemDVT_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            trangthai = 1;
        }

        private void btSuaDVT_Click(object sender, EventArgs e)
        {
            trangthai = 2;
            AnHienButton(true);
            tbMaDVT.Enabled = false;
        }

        private void btXoaDVT_Click(object sender, EventArgs e)
        {            
            if (MessageBox.Show("Bạn Có Muốn Xóa Đơn Vị Tính [" + tbTenDVT.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (donvitinhbl.XoaDVT(tbMaDVT.Text))
                    FrmDonViTinh_Load(sender, e);
            }
        }

        private void btOk_Click(object sender, EventArgs e)
        {
            try
            {
                DonViTinhBO donvitinhbo = LayThongTinDVT();
                if (trangthai == 1)
                {
                    if (donvitinhbl.ThemDVT(donvitinhbo))
                        FrmDonViTinh_Load(sender, e);
                }
                if (trangthai == 2)
                {
                    if (donvitinhbl.SuaDVT(donvitinhbo))
                        FrmDonViTinh_Load(sender, e);
                }


            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void gvDVT_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaDVT.Text = gvDVT.Rows[row].Cells["MaDVT"].Value.ToString();
                tbTenDVT.Text = gvDVT.Rows[row].Cells["TenDVT"].Value.ToString();
            }
            catch
            {
            }
        }


    }
}
